<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The Avatar class for table 'avatar'.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Adds the 'delete' function.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Definitions.
// Column of status reserves.
/*
define('AVATAR_QUALITY_TINY',		1);			// 48 * 48
define('AVATAR_QUALITY_NORMAL',		2);			// 180 * 180?
define('AVATAR_QUALITY_LARGE',		3);			// 256 * 256
*/

define('AVATAR_TINY_SIZE',			48);
define('AVATAR_NORMAL_SIZE',		180);
define('AVATAR_LARGE_SIZE',			256);

require_once INSTALLDIR . '/classes/Memcached_DataObject.php';

class Avatar extends Memcached_DataObject
{
	public $__table = 'avatar';				// table name
	public $id;								// int(4)  primary_key not_null
	public $user_id;						// int(4)  not_null foreign_key
	public $status;							// tinyint(4)
	public $size;							// tinyint(4)
	public $mimetype;						// tinyint(4)
	public $avatarname;						// varchar(255)  not_null
	public $modified;						// timestamp()  not_null
	
	// Static get
	function staticGet($key, $value = null)
	{
		return Memcached_DataObject::staticGet('Avatar', $key, $value); 
	}
	
	/**
	 * Delete the poster both in database and filesystem.
	 */
	function delete()
	{
		@unlink($this->avatarpath());
		
		parent::delete();
	}
	
	/**
	 * Get the avatar path.
	 */
	function avatarpath()
	{
		return Avatar::path($this->avatarname);
	}
	
	/**
	 * Get the avatar url.
	 */
	function avatarurl()
	{
		return Avatar::url($this->avatarname);
	}
	
	/**
	 * Get the specific avatar object.
	 */
	static function getAvatarBySize($user_id, $size)
	{
		return Memcached_DataObject::pkeyGet('Avatar', array('user_id' => $user_id,
															 'size' => $size));
	}
	
	/**
	 * The barename of the file.
	 */
	static function filename($id, 
							 $extension, 
							 $size, 
							 $extra = null)
	{
		if ($size) {
			return $size . '-' . $id . (($extra) ? ('-' . $extra) : '') . $extension;
		} else {
			return 'original-' . $id . (($extra) ? ('-' . $extra) : '') . $extension;
		}
	}
	
	/**
	 * The absolute path including the barename. (../xx.jpg).
	 */
	static function path($filename)
	{
		$dir = slh_config('avatar', 'dir');
		
		if ($dir[strlen($dir) - 1] != '/') {
			$dir .= '/';
		}
		
		return $dir . $filename;
	}
	
	/**
	 * The unique url.
	 */
	static function url($filename)
	{
		$path = slh_config('avatar', 'path');
		
		if ($path[strlen($path) - 1] != '/') {
			$path .= '/';
		}
		
		if ($path[0] != '/') {
			$path = '/' . $path;
		}
		
		// FIXME: The extra server for avatar.
		$server = slh_config('avatar', 'server');
		
		return 'http://' . $server . $path . $filename;
	}
	
	// Fake default image.
	static function defaultImage($size)
	{
// FIXME: The path!
		return slh_full_path('theme/base/image/avatar/') . $size . '-default.gif';
	}
}

?>